<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">药品订单管理</span>
    <el-button @click="beforeAddOrder" type="primary" style="float:right;margin-top:13px;">新建订单</el-button>
  </div>
  <!-- 新增订单弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input  v-model="saveOrderForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户ID">
            <el-input  v-model="saveOrderForm.userId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="总金额">
            <el-input  v-model="saveOrderForm.totalPrice"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单状态">

            <el-select placeholder="请选择" v-model="saveOrderForm.status">
              <el-option
                   label="测试1" :value="1"></el-option>

            </el-select>

          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="配送地址">
            <el-input  v-model="saveOrderForm.deliveryAddress"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input v-model="saveOrderForm.contactPhone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveOrder">录入</el-button>
    </template>

  </el-dialog>


  <!-- 维修订单搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="订单号">
        <el-input placeholder="请输入订单号" style="width:220px;"
                  v-model="searchOrderForm.license" @keydown.enter="loadOrder"></el-input>
      </el-form-item>
      <el-form-item label="用户名" style="width:290px;">
        <el-input placeholder="请输入用户名" style="width:220px;"
                  v-model="searchOrderForm.username" @keydown.enter="loadOrder"></el-input>
      </el-form-item>



      <el-form-item label="订单状态" style="width: 290px;">
        <el-select placeholder="请选择订单状态" v-model="searchOrderForm.status" @change="loadOrder">
<!--          <el-option v-for="i in orderTypeArr" label="i.label" value="i.value" ></el-option>-->

        </el-select>
      </el-form-item>



      <el-form-item>
        <el-button  @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadOrder">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 订单列表 -->
  <el-card style="margin:20px;">
    <el-table :data="OrderArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="orderId" label="订单ID" align="center"/>
      <el-table-column prop="userId" label="用户ID" align="center"/>
      <el-table-column prop="totalPrice" label="总金额" align="center"/>
      <el-table-column prop="status" label="订单状态" align="center"
                       :formatter="orderStatusTypeFormatter"/>
      <el-table-column prop="updateTime" label="订单更新时间" align="center"/>
      <el-table-column prop="deliveryAddress" label="配送地址" align="center"/>
      <el-table-column prop="contactPhone" label="联系电话" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editOrder(scope.row.orderId)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteOrder(scope.row.orderId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from '@/util/request';
import {ElMessage} from "element-plus";

const user = ref(getUser());
//控制新增订单弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入药品订单');
const searchOrderForm = ref({orderId:'',userId:'',status:''});
const OrderArr = ref([]);
//定义加载订单的方法
const loadOrder= ()=>{
  let data = qs.stringify(searchOrderForm.value);
  axios.get('/api/order/select?'+data).then((response)=>{
    if(response.data.code==2000){
      OrderArr.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
// const materialTypeArr = ref([]);
// const itemTypeArr = ref([]);
// const addrTypeArr = ref([]);

//一进到页面立刻执行调用加载订单的方法
// onMounted(()=>{
//   loadDictOptions(materialTypeArr,'repair_material');
//   loadDictOptions(itemTypeArr,'repair_item');
//   loadDictOptions(addrTypeArr,'repair_addr');
//
// });
const orderStatusTypeFormatter = (row,column,cellValue,index)=>{
  //如果字典项数组没有值,直接返回当前单元格的值,比如 10
  // if (!materialTypeArr.value){
  //   return cellValue;
  // }
  // for (let  i of materialTypeArr.value){
  //   if (i.value == cellValue){
  //     cellValue = i.label;
  //   }
  // }
  return 1;
}
// const itemTypeFormatter = (row,column,cellValue,index)=>{
//   //如果字典项数组没有值,直接返回当前单元格的值,比如 10
//   if (!itemTypeArr.value){
//     return cellValue;
//   }
//   for (let  i of itemTypeArr.value){
//     if (i.value == cellValue){
//       cellValue = i.label;
//     }
//   }
//   return cellValue;
// }
// const addrTypeFormatter = (row,column,cellValue,index)=>{
//   //如果字典项数组没有值,直接返回当前单元格的值,比如 10
//   if (!addrTypeArr.value){
//     return cellValue;
//   }
//   for (let  i of addrTypeArr.value){
//     if (i.value == cellValue){
//       cellValue = i.label;
//     }
//   }
//   return cellValue;
// }
const resetSearch = ()=>{
  searchOrderForm.value = {};
  loadOrder();
}
//一进到页面立即执行调用加载订单的方法
onMounted(()=>{
  loadOrder();
})
//对表格中订单状态显示的文字进行转换
const materialTypeFormatter = (row,colum,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '已完成';
  }else if(cellValue == 0){
    cellValue = '维修中';
  }
  return cellValue;
}



//定义对象用来保存新增订单弹窗表单的所有数据
const saveOrderForm = ref({
  username:'',
  orderId:'',
  userId:'',
  totalPrice:'',
  status:'',
  deliveryAddress:'',
  contactPhone:''
});
const handleClose = ()=>{
  if (confirm('您确定要关闭弹窗吗')){
    dialogVisible.value = false;//关闭弹窗
    saveOrderForm.value = {};//清空表单数据
  }
}


//定义保存订单的方法
const saveOrder = ()=>{
  //对数据进行校验
  if(!saveOrderForm.value.userId){
    ElMessage.error('userId不能为空!');
    return;
  }
  let data = qs.stringify(saveOrderForm.value);
  axios.post('/api/order/create',data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('保存成功!');
      //保存成功:关弹窗+清除表单数据+重新加载所有订单数据(新增数据才会显示)
      dialogVisible.value = false;
      saveOrderForm.value = {};
      loadOrder();
    }else{
      //数据库里的code和license字段设置了唯一,如果重复插入会操作失败,所以可以设置更确切的提示语句
      ElMessage.error('该订单正在维护，车牌号不允许重复插入!');
    }
  })
}
//
//定义点击"新建订单"按钮时调用的方法 出弹窗+改标题+清空表单数据
const beforeAddOrder = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新建药品订单';
  saveOrderForm.value = {};

}
//定义编辑订单的方法 出弹窗+改标题+回显数据
const editOrder = (orderId)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑订单';
  //根据当前订单id回显弹窗表单中此订单的详细数据
  axios.get('/api/order/select?id='+orderId).then((response)=>{
    if (response.data.code==2000){
      saveOrderForm.value = response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//定义删除订单的方法
const deleteOrder = (orderId)=>{
  if(confirm('您确认要删除该订单吗?')){
    axios.post('/api/order/delete/'+orderId).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功!');
        //删除成功后,需要重新加载所有的订单数据列表!
        loadOrder();
      }else {
        ElMessage.error(response.data.msg);
      }

    })
  }
}

</script>